package app.server.project.core.memory;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MemoryService {
    private static final long Gib = 1024 * 1024 * 1024;
    private static final long Mib = 1024 * 1024;
    private static final long PrintTime = 1000 * 60 * 10;

    @Scheduled(fixedRate = PrintTime)
    public void printMemInfo() {
        var free = Runtime.getRuntime().freeMemory();
        var total = Runtime.getRuntime().totalMemory();
        var max = Runtime.getRuntime().maxMemory();
        log.info("====空闲内存:{} m===使用内存:{} m===目前全部内存:{} m====最大内存:{} m======内存使用比例:{}%", free / Mib, (total-free)/Mib,total / Mib, max / Mib, (total - free) * 100 / total);
        log.info("====空闲内存:{} G===使用内存:{} m===目前全部内存:{} G====最大内存:{} G======内存使用比例:{}%", free / Gib, (total-free)/Gib,total / Gib, max / Gib, (total - free) * 100 / total);
    }
}
